﻿/*
 * HostListRMS.java
 *
 * Created on 2007年10月22日, 下午 3:57
 *
 */

package org.uranus.core.dataHandler;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 * Copyright 2007 Hsiang-Yu Wang<br>
 * This file is distributed under the terms of the GNU General Public License<br>
 *<br>
 *<pre>
 * --------------------License notice--------------------
 * GNU GENERAL PUBLIC LICENSE
 * Version 3, 29 June 2007
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not,
 * visit: http://www.gnu.org/licenses/gpl.txt
 * --------------------License notice--------------------</pre>
 *
 * @author Hsiang-Yu Wang
 */
public class HostListRMS extends RMS{
    
    private String[] list;
    /** Creates a new instance of HostListRMS */
    public HostListRMS() {
        super.rmsName = "Hosts_list";
        setList(null);
        if(findRecord(rmsName)){
            load(rmsName);
        }else{
            setList(new String[0]);            
        }
        
    }
    
    
    
    public void addRecord(String hostName){
        String[] newList = new String[getList().length + 1];
        newList[getList().length] = hostName;
        System.arraycopy(list, 0, newList, 0, newList.length - 1);
        
        setList(newList);
        save();
    }
    
    public void delRecord(String hostName){
        String[] newList = new String[getList().length - 1];
        int k = 0;
        
        
        //  Very dangerous !!         
        for(int i = 0 ; i < getList().length ; i++){
            if(getList()[i].equals(hostName)){
                
                continue;
            }
            newList[k++] = getList()[i];
        }
        list = newList;
        save();
        try {
            if(list.length == 0)
                RecordStore.deleteRecordStore(rmsName);
              
            RecordStore.deleteRecordStore("_host_" + hostName);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }
    
    public HostInfoData getSelectedHost(int index){
        return new HostInfoData(list[index], false);
        
    }
    
    public void write(DataOutputStream out) throws IOException {
        for(int i = 0 ; i < getList().length ; i++){
            out.writeUTF(getList()[i]);
        }
    }

    public void read(DataInputStream in) throws IOException {
        Vector v = new Vector();
        while(in.available() != 0){
            v.addElement(new String(in.readUTF()));
        }
        setList(new String[v.size()]);
        int i = 0;
        for(Enumeration e = v.elements() ; e.hasMoreElements() ;){
            String s = (String)e.nextElement();
            getList()[i++] = s.toString();
        }
    }

    public String[] getList() {
        return list;
    }

    public void setList(String[] list) {
        this.list = list;
    }

    public void save() {
        save(rmsName);
    }
    
    
}
